/*
wgtsettings.cpp
(c) 2010 Yury Hamenkov
This file is part of qWordCards.

qWordCards is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

qWordCards is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with qWordCards. If not, see <http://www.gnu.org/licenses/>.
*/

#include "wgtsettings.h"
#include "preferences.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QCoreApplication>
#include <QList>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QMap>
#include <QPair>

using namespace qWordCards;

WGTSettings::WGTSettings(QWidget *parent) : QDialog(parent) {
	this->setModal(true);
	this->setWindowTitle(tr("Program settings"));
	QWidget* dummy = 0;
	dummy = new QWidget();

	this->setObjectName("Settings widget");

	this->FDLFontDialog = new QFontDialog(this);
	this->CDLColorDialog = new QColorDialog(this);

	this->TWGTabs = new QTabWidget();
	this->TWGTabs->setObjectName("TWGTabs");
	this->WGTBehaviour = new QWidget();
	this->WGTBehaviour->setObjectName("WGTBehaviour");
	this->WGTAppearance = new QWidget();
	this->WGTAppearance->setObjectName("WGTAppearance");
	this->TWGTabs->addTab(this->WGTBehaviour, tr("Behaviour"));
	this->TWGTabs->addTab(this->WGTAppearance, tr("Appearance"));

	this->WGTBottomPanel = new QWidget();
	this->WGTBottomPanel->setFixedHeight(50);
	this->BTNSaveButton = new QPushButton(WGTBottomPanel);
	this->BTNSaveButton->setText(tr("Save"));
	this->BTNSaveButton->move(10,10);
	QObject::connect(this->BTNSaveButton, SIGNAL(clicked()), this, SLOT(sl_BTNSaveClicked()));
	this->BTNCloseButton = new QPushButton(WGTBottomPanel);
	this->BTNCloseButton->setText(tr("Close"));
	this->BTNCloseButton->move(100, 10);
	QObject::connect(this->BTNCloseButton, SIGNAL(clicked()), this, SLOT(sl_BTNCloseClicked()));

	QVBoxLayout* mainLayout = new QVBoxLayout();
	mainLayout->setContentsMargins(0, 0, 0, 0);
	mainLayout->addWidget(this->TWGTabs);
	mainLayout->addWidget(this->WGTBottomPanel);
	this->setLayout(mainLayout);

	// Behaviour tab
	this->LBLWordChangeInterval = new QLabel();
	this->LBLWordChangeInterval->setText(tr("Word change interval (sec):"));
	this->SBXWordChangeInterval = new QSpinBox();
	this->SBXWordChangeInterval->setMinimum(1);
	this->SBXWordChangeInterval->setMaximum(3600);
	QObject::connect(this->SBXWordChangeInterval, SIGNAL(valueChanged(int)), this, SLOT(sl_SBXWordChangeIntervalValueChanged(int)));

	this->CHXWordShowDuration = new QCheckBox();
	this->CHXWordShowDuration->setText(tr("Hide card after this time (sec):"));
	QObject::connect(this->CHXWordShowDuration, SIGNAL(stateChanged(int)), this, SLOT(sl_CHXWordShowDurationStateChanged(int)));
	this->SBXWordShowDuration = new QSpinBox();
	this->SBXWordShowDuration->setMinimum(1);
	QObject::connect(this->SBXWordShowDuration, SIGNAL(valueChanged(int)), this, SLOT(sl_SBXWordShowDurationValueChanged(int)));

	this->CHXTranslationShowInterval = new QCheckBox();
	this->CHXTranslationShowInterval->setText(tr("Show translation after this time (sec):"));
	QObject::connect(this->CHXTranslationShowInterval, SIGNAL(stateChanged(int)), this, SLOT(sl_CHXTranslationShowIntervalStateChanged(int)));
	this->SBXTranslationShowInterval = new QSpinBox();
	this->SBXTranslationShowInterval->setMinimum(0);
	QObject::connect(this->SBXTranslationShowInterval, SIGNAL(valueChanged(int)), this, SLOT(sl_SBXTranslationShowIntervalValueChanged(int)));

	QGridLayout* bLayout = new QGridLayout();
	bLayout->setContentsMargins(10, 10, 10, 10);
	bLayout->addWidget(this->LBLWordChangeInterval, 0, 0);
	bLayout->addWidget(this->SBXWordChangeInterval, 0, 1);
	bLayout->addWidget(this->CHXWordShowDuration, 1, 0);
	bLayout->addWidget(this->SBXWordShowDuration, 1, 1);
	bLayout->addWidget(this->CHXTranslationShowInterval, 2, 0);
	bLayout->addWidget(this->SBXTranslationShowInterval, 2, 1);
	bLayout->addWidget(dummy, 3, 0);

	this->WGTBehaviour->setLayout(bLayout);

	// Appearance tab
	this->CHXShowToolbar = new QCheckBox();
	this->CHXShowToolbar->setText(tr("Show toolbar"));

	this->LBLLanguage = new QLabel();
	this->LBLLanguage->setText(tr("Language: "));
	this->CBXLanguage = new QComboBox();

	this->LBLMainWindowBackColorLabel = new QLabel();
	this->LBLMainWindowBackColorLabel->setText(tr("Main window back color:"));
	this->FRMMainWindowBackColor = new QFrame();
	this->FRMMainWindowBackColor->setLineWidth(1);
	this->FRMMainWindowBackColor->setFrameStyle(QFrame::Panel);
	this->FRMMainWindowBackColor->setFrameShadow(QFrame::Plain);
	this->FRMMainWindowBackColor->setAutoFillBackground(true);
	this->BTNChooseMainWindowBackColor = new QPushButton();
	this->BTNChooseMainWindowBackColor->setText("...");
	this->BTNChooseMainWindowBackColor->setFixedWidth(50);
	QObject::connect(this->BTNChooseMainWindowBackColor, SIGNAL(clicked()), this, SLOT(sl_BTNChooseMainWindowBackColorClicked()));

	QString font(tr("Font"));
	QString color(tr("Color"));

	this->LBLWordFont = new QLabel();
	this->LBLWordFont->setText(tr("Word font:"));
	this->LBLWordFontExample = new QLabel();
	this->LBLWordFontExample->setText("Example");
	this->BTNWordFont = new QPushButton();
	this->BTNWordFont->setText(font);
	this->BTNWordFont->setFixedWidth(50);
	QObject::connect(this->BTNWordFont, SIGNAL(clicked()), this, SLOT(sl_BTNWordFontClicked()));
	this->BTNWordColor = new QPushButton();
	this->BTNWordColor->setText(color);
	this->BTNWordColor->setFixedWidth(50);
	QObject::connect(this->BTNWordColor, SIGNAL(clicked()), this, SLOT(sl_BTNWordColorClicked()));

	this->LBLTranscriptionFont = new QLabel();
	this->LBLTranscriptionFont->setText(tr("Transcription font:"));
	this->LBLTranscriptionFontExample = new QLabel();
	this->LBLTranscriptionFontExample->setText("Example");
	this->BTNTranscriptionFont = new QPushButton();
	this->BTNTranscriptionFont->setText(font);
	this->BTNTranscriptionFont->setFixedWidth(50);
	QObject::connect(this->BTNTranscriptionFont, SIGNAL(clicked()), this, SLOT(sl_BTNTranscriptionFontClicked()));
	this->BTNTranscriptionColor = new QPushButton();
	this->BTNTranscriptionColor->setText(color);
	this->BTNTranscriptionColor->setFixedWidth(50);
	QObject::connect(this->BTNTranscriptionColor, SIGNAL(clicked()), this, SLOT(sl_BTNTranscriptionColorClicked()));

	this->LBLTranslationFont = new QLabel();
	this->LBLTranslationFont->setText(tr("Translation font:"));
	this->LBLTranslationFontExample = new QLabel();
	this->LBLTranslationFontExample->setText("Example");
	this->BTNTranslationFont = new QPushButton();
	this->BTNTranslationFont->setText(font);
	this->BTNTranslationFont->setFixedWidth(50);
	QObject::connect(this->BTNTranslationFont, SIGNAL(clicked()), this, SLOT(sl_BTNTranslationFontClicked()));
	this->BTNTranslationColor = new QPushButton();
	this->BTNTranslationColor->setText(color);
	this->BTNTranslationColor->setFixedWidth(50);
	QObject::connect(this->BTNTranslationColor, SIGNAL(clicked()), this, SLOT(sl_BTNTranslationColorClicked()));

	QGridLayout* aLayout = new QGridLayout();
	aLayout->setObjectName("Appearance layout");
	aLayout->setContentsMargins(10, 10, 10, 10);
	aLayout->addWidget(this->CHXShowToolbar, 0, 0);

	aLayout->addWidget(this->LBLLanguage, 1, 0);
	aLayout->addWidget(this->CBXLanguage, 1, 1);

	aLayout->addWidget(this->LBLMainWindowBackColorLabel, 2, 0);
	aLayout->addWidget(this->FRMMainWindowBackColor, 2, 1);
	aLayout->addWidget(this->BTNChooseMainWindowBackColor, 2, 2);

	aLayout->addWidget(this->LBLWordFont, 3, 0);
	aLayout->addWidget(this->LBLWordFontExample, 3, 1);
	aLayout->addWidget(this->BTNWordFont, 3, 2);
	aLayout->addWidget(this->BTNWordColor, 3, 3);

	aLayout->addWidget(this->LBLTranscriptionFont, 4, 0);
	aLayout->addWidget(this->LBLTranscriptionFontExample, 4, 1);
	aLayout->addWidget(this->BTNTranscriptionFont, 4, 2);
	aLayout->addWidget(this->BTNTranscriptionColor, 4, 3);

	aLayout->addWidget(this->LBLTranslationFont, 5, 0);
	aLayout->addWidget(this->LBLTranslationFontExample, 5, 1);
	aLayout->addWidget(this->BTNTranslationFont, 5, 2);
	aLayout->addWidget(this->BTNTranslationColor, 5, 3);

	dummy = new QWidget();
	aLayout->addWidget(dummy, 6, 0);
	this->WGTAppearance->setLayout(aLayout);
	
	// Load data
	this->SBXWordChangeInterval->setValue(Preferences::wordChangeInterval / 1000);

	if (Preferences::wordShowDuration < 0) {
		this->CHXWordShowDuration->setChecked(false);
		this->SBXWordShowDuration->setValue(0);
		this->SBXWordShowDuration->setEnabled(false);
	} else {
		this->CHXWordShowDuration->setChecked(true);
		this->SBXWordShowDuration->setEnabled(true);
		this->SBXWordShowDuration->setValue(Preferences::wordShowDuration / 1000);
	}

	if (Preferences::showTranslationInterval < 0) {
		this->CHXTranslationShowInterval->setChecked(false);
		this->SBXTranslationShowInterval->setEnabled(false);
		this->SBXTranslationShowInterval->setValue(0);
	} else {
		this->CHXTranslationShowInterval->setChecked(true);
		this->SBXTranslationShowInterval->setEnabled(true);
		this->SBXTranslationShowInterval->setValue(Preferences::showTranslationInterval / 1000);
	}

	if (Preferences::showToolbar) {
		this->CHXShowToolbar->setChecked(true);
	}

	this->FNTWordFont = Preferences::wordFont;
	this->CLRWordColor = Preferences::wordColor;

	this->FNTTranscriptionFont = Preferences::transcriptionFont;
	this->CLRTranscriptionColor = Preferences::transcriptionColor;

	this->FNTTranslationFont = Preferences::translationFont;
	this->CLRTranslationColor = Preferences::translationColor;
	
	this->CLRMainWindowBackColor = Preferences::mainWindowBackColor;
	QPalette pal = this->FRMMainWindowBackColor->palette();
	pal.setColor(QPalette::Active, QPalette::Background, this->CLRMainWindowBackColor);
	pal.setColor(QPalette::Inactive, QPalette::Background, this->CLRMainWindowBackColor);
	this->FRMMainWindowBackColor->setPalette(pal);

	// Fill "Language" combobox
	this->CBXLanguage->addItem("eng", "");
	QDir dir("./");
	QList<QFileInfo> files = dir.entryInfoList(QDir::Files);
	foreach(QFileInfo f, files) {
		if (f.isFile()) {
			if (f.suffix() == "qm") {
				this->CBXLanguage->addItem(f.completeBaseName(), f.absoluteFilePath());
			}
		}
	}
	UpdateWordExamples();


}

void WGTSettings::UpdateWordExamples() {
	QMap< QLabel*, QPair<QFont, QColor> > data;
	QPair<QFont, QColor> p1 (this->FNTWordFont, this->CLRWordColor);
	QPair<QFont, QColor> p2 (this->FNTTranscriptionFont, this->CLRTranscriptionColor);
	QPair<QFont, QColor> p3 (this->FNTTranslationFont, this->CLRTranslationColor);
	data.insert(this->LBLWordFontExample, p1);
	data.insert(this->LBLTranscriptionFontExample, p2);
	data.insert(this->LBLTranslationFontExample, p3);

	foreach (QLabel* l, data.keys()) {
		QFont f = data[l].first;
		QColor c = data[l].second;

		QString t;

		t = "<style type=\"text/css\"> p { ";
		t += "font-size: " + QString::number(f.pointSize()) + "pt; ";
		if (f.bold()) {
			t += "font-weight: bold; ";
		}
		if (f.italic()) {
			t += "font-style: italic; ";
		}
		if (f.underline()) {
			t += "text-decoration: underline; ";
		}
		t += "color: " + c.name() + "; ";
		t += " }</style>";

		t += "<p>Example</p>";

		//qDebug() << t;

		l->setText(t);
	}
}

void WGTSettings::sl_BTNWordFontClicked() {
	this->FDLFontDialog->setCurrentFont(this->FNTWordFont);
	this->FDLFontDialog->exec();
	this->FNTWordFont = this->FDLFontDialog->selectedFont();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNTranscriptionFontClicked() {
	this->FDLFontDialog->setCurrentFont(this->FNTTranscriptionFont);
	this->FDLFontDialog->exec();
	this->FNTTranscriptionFont = this->FDLFontDialog->selectedFont();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNTranslationFontClicked() {
	this->FDLFontDialog->setCurrentFont(this->FNTTranslationFont);
	this->FDLFontDialog->exec();
	this->FNTTranslationFont = this->FDLFontDialog->selectedFont();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNWordColorClicked() {
	this->CDLColorDialog->setCurrentColor(this->CLRWordColor);
	this->CDLColorDialog->exec();
	this->CLRWordColor = this->CDLColorDialog->selectedColor();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNTranscriptionColorClicked() {
	this->CDLColorDialog->setCurrentColor(this->CLRTranscriptionColor);
	this->CDLColorDialog->exec();
	this->CLRTranscriptionColor = this->CDLColorDialog->selectedColor();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNTranslationColorClicked() {
	this->CDLColorDialog->setCurrentColor(this->CLRTranslationColor);
	this->CDLColorDialog->exec();
	this->CLRTranslationColor = this->CDLColorDialog->selectedColor();
	UpdateWordExamples();
}

void WGTSettings::sl_BTNChooseMainWindowBackColorClicked() {
	this->CDLColorDialog->setCurrentColor(this->CLRMainWindowBackColor);
	this->CDLColorDialog->exec();
	this->CLRMainWindowBackColor = this->CDLColorDialog->selectedColor();
	
	QPalette pal = this->FRMMainWindowBackColor->palette();
	pal.setColor(QPalette::Active, QPalette::Background, this->CLRMainWindowBackColor);
	pal.setColor(QPalette::Inactive, QPalette::Background, this->CLRMainWindowBackColor);
	this->FRMMainWindowBackColor->setPalette(pal);
}


void WGTSettings::sl_SBXWordChangeIntervalValueChanged(int i) {
	this->SBXWordShowDuration->setMaximum(i);
}

void WGTSettings::sl_SBXWordShowDurationValueChanged(int i) {
	
}

void WGTSettings::sl_SBXTranslationShowIntervalValueChanged(int i) {
	
}


void WGTSettings::sl_CHXWordShowDurationStateChanged(int i) {
	this->SBXWordShowDuration->setEnabled(this->CHXWordShowDuration->isChecked());
}

void WGTSettings::sl_CHXTranslationShowIntervalStateChanged(int i) {
	this->SBXTranslationShowInterval->setEnabled(this->CHXTranslationShowInterval->isChecked());
}

void WGTSettings::sl_BTNSaveClicked() {
	// Save
	Preferences::wordChangeInterval = this->SBXWordChangeInterval->value() * 1000;
	if (this->CHXWordShowDuration->isChecked()) {
		Preferences::wordShowDuration = this->SBXWordShowDuration->value() * 1000;
	} else {
		Preferences::wordShowDuration = -1;
	}
	if (this->CHXTranslationShowInterval->isChecked()) {
		Preferences::showTranslationInterval = this->SBXTranslationShowInterval->value() * 1000;
	} else {
		Preferences::showTranslationInterval = -1;
	}
	
	Preferences::showToolbar = this->CHXShowToolbar->isChecked();
	Preferences::mainWindowBackColor = this->CLRMainWindowBackColor;
	Preferences::wordFont = this->FNTWordFont;
	Preferences::wordColor = this->CLRWordColor;
	Preferences::transcriptionFont = this->FNTTranscriptionFont;
	Preferences::transcriptionColor = this->CLRTranscriptionColor;
	Preferences::translationFont = this->FNTTranslationFont;
	Preferences::translationColor = this->CLRTranslationColor;
	
	
	
	
	this->close();
}

void WGTSettings::sl_BTNCloseClicked() {
	this->close();
}

